import Core.WindFunctions as Wind
import Core.Gadget as Gadget

def Generate_Manager_Key(name, resume):
    if len(resume) >= 20:
        return name + "_" + resume[:20]
    else:
        return name + "_" + resume


# 相当于每次都全量更新任职信息，只有核心信息，没有补充信息
def Download_MutualFund_Manager_Info(database, cold_start=False):
    #
    def Process_MutualFund_Manager_Detail(database, symbols, datetime_update=None, order=1):
        #
        if datetime_update == None:
            datetime_update = datetime.datetime.now()
        #
        partial_symbols = symbols
        #
        fields = []
        fields.append("fund_fundmanager")  # 基金经理（现任）
        fields.append("fund_manager_resume")  # 简历
        fields.append("fund_manager_birthyear")  # 出生年份
        # fields.append("xxxxxxx")  # 历任基金代码
        fields.append("fund_predfundmanager")  # 历任基金经理
        fields.append("fund_manager_fundcodes")  # 任职基金代码（当前）
        fields.append("fund_manager_totalnetasset")  # 任职基金总规模（当前）
        fields.append("fund_manager_awardrecord")  # 获奖记录
        fields.append("fund_manager_managerworkingyears")  # 基金经理从业时间
        fields.append("fund_manager_gender")  # 性别
        fields.append("fund_manager_age")  # 年龄
        fields.append("fund_manager_startdate")  # 任职日期
        fields.append("fund_manager_geometricannualizedyield")  # 几何年化收益
        #
        data = Wind.WSS(partial_symbols, fields, params={"order": order, "returnType": 1})
        documents = []
        for i_symbol in range(len(partial_symbols)):
            symbol = partial_symbols[i_symbol]
            d = data[i_symbol]
            current_manager = d["fund_fundmanager"]
            if current_manager == None:
                continue
            #
            multiple_managers = current_manager.split(",")

            if len(multiple_managers) < order:  # 当前基金只有1位经理，但是参数order为2
                continue

            manager_name = multiple_managers[order-1]
            #
            document = {}
            document["total_manager_name"] = d["fund_fundmanager"]
            document["manager_name"] = manager_name
            document["date"] = document["datetime"] = datetime_update

            age = d["fund_manager_age"]
            if age != None:    # 生日默认为减去年龄的1月1日
                document["birthdate"] = datetime.datetime(datetime_update.year - age , 1,1)
            else:
                document["birthdate"] = None

            working_years = d["fund_manager_managerworkingyears"]
            document["working_years"] = working_years
            if working_years != None:  # 利用工作时间倒推入行时间
                document["datetime1"] = datetime.datetime(datetime_update.year - int(working_years), 1, 1)
            else:
                document["datetime1"] = None

            document["manager_average_geometric_return"] = d["fund_manager_geometricannualizedyield"]
            document["gender"] = d["fund_manager_gender"]
            document["awards"] = d["fund_manager_awardrecord"]
            document["current_manager_aum"] = d["fund_manager_totalnetasset"]
            document["resume"] = d["fund_manager_resume"]
            document["current_manage_fund_symbol"] = d["fund_manager_fundcodes"]
            document["disclosure_by_symbol"] = symbol
            #
            document["key2"] = Generate_Manager_Key(manager_name, document["resume"])
            documents.append(document)
            if order == 2:
                a = 0
            print(order, document)
        #
        # database.Upsert_Many("MutualFund", "Manager", {}, documents)

    def Process_MutualFund_Manager_Tenure(database, symbols):
        #
        partial_symbols = symbols
        #
        fields = []
        fields.append("fund_predfundmanager")  # 历任基金经理
        # fields.append("fund_corp_fundmanagementcompany")  # 基金公司简称
        #
        for i_symbol in range(len(partial_symbols)):
            data = Wind.WSS(partial_symbols, fields)
            d = data[i_symbol]
            symbol = partial_symbols[i_symbol]
            # current_manager = d["fund_fundmanager"]
            str_historical_managers = d["fund_predfundmanager"]
            historical_managers = Parse_Historical_Manager(str_historical_managers)

            # 添加扩展信息
            for manager in historical_managers:
                manager["Symbol"] = symbol
                # manager["Company_Alias"] = d["fund_corp_fundmanagementcompany"]
        #
        Update_MutualFund_Manager_Tenure(database, historical_managers)

    datetime_update = datetime.datetime(2020, 11, 1)
    df_mutualfund = database.Get_Instruments_DataFrame(instrument_type="mutualfund")
    symbols = df_mutualfund["symbol"].tolist()

    #
    # fields.append("fund_fundmanager")  # 基金经理（现任）
    # fields.append("fund_fundmanageroftradedate")  # 基金经理（可查历史）
    #  params={"tradeDate": str(datetime_update)}

    # 分批提取
    step = 1
    start = 0
    for i in range(0, len(symbols), step):
        print("Download Fund Manager Process from", i, "to", i + step)
        if i < start:
            continue
        partial_symbols = symbols[i:i + step]
        print(partial_symbols)
        # 测试用
        # fields = []
        # fields.append("fund_fundmanager")  # 基金经理（现任）
        # data = Wind.WSS(partial_symbols, fields)

        # 取出基金经理 第一位和第二位
        Process_MutualFund_Manager_Detail(database, partial_symbols, order=1)
        Process_MutualFund_Manager_Detail(database, partial_symbols, order=2)
        #
        # Process_MutualFund_Manager_Tenure(database, partial_symbols)
        a = 0

    # 更新基金经理池子
    # Update_MutualFund_Manager(database, "")


def Download_MutualFund_Manager_Tenure(database, datetime_update, cold_start=False):
    #
    def Process_MutualFund_Manager_Tenure(database, symbols, datetime_update):
        #
        partial_symbols = symbols
        #
        fields = []
        fields.append("fund_predfundmanager")  # 历任基金经理
        # fields.append("fund_corp_fundmanagementcompany")  # 基金公司简称
        data = Wind.WSS(partial_symbols, fields)
        #
        documents = []
        for i_symbol in range(len(partial_symbols)):
            d = data[i_symbol]
            symbol = partial_symbols[i_symbol]
            # current_manager = d["fund_fundmanager"]
            str_historical_managers = d["fund_predfundmanager"]
            if str_historical_managers == None:
                print("None Manager Names", symbol)
                continue
            historical_managers = Parse_Historical_Manager(str_historical_managers)

            # 添加扩展信息
            for manager in historical_managers:
                manager["Symbol"] = symbol
                manager["date"] = datetime_update
                manager["datetime"] = datetime_update
                # manager["Company_Alias"] = d["fund_corp_fundmanagementcompany"]
                documents.append(manager)
        #
        Update_MutualFund_Manager_Tenure(database, documents)

    df_mutualfund = database.Get_Instruments_DataFrame(instrument_type="mutualfund")
    symbols = df_mutualfund["symbol"].tolist()

    # 分批提取
    step = 50
    start = 2750
    for i in range(0, len(symbols), step):
        print("Download Fund Manager Process from", i, "to", i + step)
        if i < start:
            continue
        partial_symbols = symbols[i:i + step]
        Process_MutualFund_Manager_Tenure(database, partial_symbols, datetime_update)
        a = 0


# 按照order取经理时候，会出现问题，因此进行排查修正
def Fix_Manager_Wrong_Order(database):
    #
    def Find_Max_Possible_Manager(documents):
        manager_count = {}
        for document in documents:
            raw_name = document["fund_fundmanager"]
            names = raw_name.split(",")
            for name in names:
                if name in manager_count:
                    manager_count[name] += 1
                else:
                    manager_count[name] = 1
        # 排序
        data = []
        for key, value in manager_count.items():
            data.append([key, value])
        df = pd.DataFrame(data, columns=["manager", "count"])
        df.sort_values(by="count", ascending=False, inplace=True)
        print(df)
        return df.iloc[0]["manager"]

    #
    # database.Delete("financial_data", "mutualfund_manager", {"key2": "何如_美国特许金融分析师CFA、工商管理硕士M"})
    # database.Delete("financial_data", "mutualfund_manager", {"key2": "陈正宪_硕士研究生，具有基金从业资格。曾任职于I"})
    #
    df_manager = database.GetDataFrame("financial_data", "mutualfund_manager",
                                       projection=["key2", "manager_name", "resume", "current_manage_fund_symbol"])
    count1 = len(df_manager)
    print(df_manager.dtypes)
    df_groups = df_manager.groupby("resume")
    n = len(df_groups)
    for group in df_groups:
        resume = group[0]
        df_tmp = group[1]
        # 重复的Resume
        if len(df_tmp) > 1:
            print(df_tmp)
            b = df_tmp.iloc[0]["current_manage_fund_symbol"]
            s2 = bytes.decode(b)
            s3 = b.decode()
            current_manage_fund_symbols = s3.split(",")

            fields = []
            fields.append("fund_fundmanager")  # 基金经理（现任）
            data = Wind.WSS(current_manage_fund_symbols, fields)
            max_possible_manager = Find_Max_Possible_Manager(data)

            for index, row in df_tmp.iterrows():
                duplicated_manager_name = row["manager_name"]
                potential_manager = database.GetDataFrame("financial_data", "mutualfund_manager", filter={"manager_name": duplicated_manager_name})
                print(potential_manager)
                a = 0

    df1 = df_manager.drop_duplicates(subset=["key2"], keep="first")
    df2 = df_manager.drop_duplicates(subset=["resume"], keep="first")
    print(df1)
    print(df2)
    a = 0


# str 用 \r\n 隔开
def Parse_Historical_Manager(historical_mangers, symbol=None):
    result = []
    managers = historical_mangers.split("\r\n")
    for manager in managers:
        # '王亚伟(20011218-20050412)'
        name_tenure = manager.split("(")
        name = name_tenure[0]
        #
        str_tenure = name_tenure[1][:-1]  # 最后一位是 ")"

        # 解析日期
        # 20011218-20050412 , '20150107至今'
        if "至今" in str_tenure:
            tmp_str_tenures = str_tenure[:-2]
            datetime1 = datetime.datetime.strptime(tmp_str_tenures, '%Y%m%d').date()
            datetime2 = datetime.datetime(2100, 1, 1).date()
        else:
            str_tenures = str_tenure.split("-")
            datetime1 = datetime.datetime.strptime(str_tenures[0], '%Y%m%d').date()
            datetime2 = datetime.datetime.strptime(str_tenures[1], '%Y%m%d').date()
        #
        parsed_data = {}
        parsed_data["Manager_Name"] = name
        parsed_data["DateTime1"] = datetime1
        parsed_data["DateTime2"] = datetime2
        #
        if symbol:
            parsed_data["Symbol"] = symbol
        #
        result.append(parsed_data)
    #
    return result


# data = []
# {Name , Symbol, DateTime1, DateTime2}
def Update_MutualFund_Manager_Tenure(database, data):
    for i in range(len(data)):
        symbol = data[i]["Symbol"]
        name = data[i]["Manager_Name"]
        datetime1 = data[i]["DateTime1"]
        #
        data[i]["key2"] = name + "_" + symbol + "_" + Gadget.ToDateString(datetime1)
    #
    database.Upsert_Many("MutualFund","Manager_Tenure", {}, data)


# 通过任职信息，确认所有基金经理
def Get_Total_Fund_Managers(database):
    #
    df_tenure = database.GetDataFrame("mutualfund", "manager_tenure", )


if __name__ == '__main__':
    #
    # ---Connect to DataBase, Find Series 连接数据库---
    from Core.Config import *

    pathfilename = os.getcwd() + "\..\Config\config2.json"
    config = Config(pathfilename)
    database = config.DataBase("JDMySQL")
    realtime = config.RealTime()

    # 启动Wind API
    Wind.w.start()
    datetime_update = datetime.datetime(2020, 9, 1)

    # ---更新基金经理信息---
    # Download_MutualFund_Manager_Tenure(database, datetime_update)
    # Download_MutualFund_Manager_Info(database)
    # Fix_Manager_Wrong_Order(database)
